﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Phone.Framework.Net;
using System.Windows.Media.Imaging;
using Phone.Framework.Threads;

namespace Phone.Framework.Data
{

    public class BingImageSearch
    {

        public static void Search(string query, Action<ImageSearchResponse> callback)
        {
            Search(query, 1, callback);
           
        }

        public static void Search(string query, int count, Action<ImageSearchResponse> callback)
        {

            WebUtils.DoGet<ImageSearchResponse>
(string.Format("http://api.bing.net/json.aspx?AppId=95407C3D77EFF90C03795354D1B3C862539AC87E&Query={0}&Sources=Image&Version=2.0&Market=en-us&Image.Count={1}&Image.Offset=0", query,count), null,
callback);
        }

        public static void SearchBingImage(Image image,string key)
        {
            SearchBingImage(image, 1, key);
        }

        public static void SearchBingImage(Image image, int count, string key)
        {
            BingImageSearch.Search(key, count,new Action<ImageSearchResponse>((ImageSearchResponse s) =>
                {
                    if (s.SearchResponse.Image.Total == 0) return;
                    var result = s.SearchResponse.Image.Results[0];
                   // UrlToSource(image, result.Thumbnail.Url);
                    UIThread.Invoke(() => {
                        LowProfileImageLoader.SetUriSource(image, new Uri(result.Thumbnail.Url));
                    });
                   
                }));
        }

        public static void UrlToSource(Image image, string url)
        {
            UIThread.Invoke(() =>
            {
                BitmapImage a = new BitmapImage();
                a.CreateOptions = BitmapCreateOptions.BackgroundCreation;

                a.UriSource = new Uri(url);
                a.ImageOpened += delegate
                {
                    image.Source = a;
                };

            });
        }
    }



    public class ImageSearchResponse
    {
        public SearchResponse SearchResponse { get; set; }
    }

    public class SearchResponse
    {
        public string Version { get; set; }

        public Query Query { get; set; }

        public ImageResponse Image { get; set; }
    }

    public class Query
    {
        public string SearchTerms { get; set; }
    }


    public partial class ImageResponse : System.ComponentModel.INotifyPropertyChanged
    {

        private uint totalField;

        private bool totalFieldSpecified;

        private uint offsetField;

        private bool offsetFieldSpecified;

        private ImageResult[] resultsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
        public uint Total
        {
            get
            {
                return this.totalField;
            }
            set
            {
                this.totalField = value;
                this.RaisePropertyChanged("Total");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool TotalSpecified
        {
            get
            {
                return this.totalFieldSpecified;
            }
            set
            {
                this.totalFieldSpecified = value;
                this.RaisePropertyChanged("TotalSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
        public uint Offset
        {
            get
            {
                return this.offsetField;
            }
            set
            {
                this.offsetField = value;
                this.RaisePropertyChanged("Offset");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool OffsetSpecified
        {
            get
            {
                return this.offsetFieldSpecified;
            }
            set
            {
                this.offsetFieldSpecified = value;
                this.RaisePropertyChanged("OffsetSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Order = 2)]
        [System.Xml.Serialization.XmlArrayItemAttribute(IsNullable = false)]
        public ImageResult[] Results
        {
            get
            {
                return this.resultsField;
            }
            set
            {
                this.resultsField = value;
                this.RaisePropertyChanged("Results");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public partial class ImageResult : System.ComponentModel.INotifyPropertyChanged
    {

        private string titleField;

        private string mediaUrlField;

        private string urlField;

        private string displayUrlField;

        private uint widthField;

        private bool widthFieldSpecified;

        private uint heightField;

        private bool heightFieldSpecified;

        private uint fileSizeField;

        private bool fileSizeFieldSpecified;

        private string contentTypeField;

        private Thumbnail thumbnailField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
        public string Title
        {
            get
            {
                return this.titleField;
            }
            set
            {
                this.titleField = value;
                this.RaisePropertyChanged("Title");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
        public string MediaUrl
        {
            get
            {
                return this.mediaUrlField;
            }
            set
            {
                this.mediaUrlField = value;
                this.RaisePropertyChanged("MediaUrl");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 2)]
        public string Url
        {
            get
            {
                return this.urlField;
            }
            set
            {
                this.urlField = value;
                this.RaisePropertyChanged("Url");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 3)]
        public string DisplayUrl
        {
            get
            {
                return this.displayUrlField;
            }
            set
            {
                this.displayUrlField = value;
                this.RaisePropertyChanged("DisplayUrl");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 4)]
        public uint Width
        {
            get
            {
                return this.widthField;
            }
            set
            {
                this.widthField = value;
                this.RaisePropertyChanged("Width");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool WidthSpecified
        {
            get
            {
                return this.widthFieldSpecified;
            }
            set
            {
                this.widthFieldSpecified = value;
                this.RaisePropertyChanged("WidthSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 5)]
        public uint Height
        {
            get
            {
                return this.heightField;
            }
            set
            {
                this.heightField = value;
                this.RaisePropertyChanged("Height");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool HeightSpecified
        {
            get
            {
                return this.heightFieldSpecified;
            }
            set
            {
                this.heightFieldSpecified = value;
                this.RaisePropertyChanged("HeightSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 6)]
        public uint FileSize
        {
            get
            {
                return this.fileSizeField;
            }
            set
            {
                this.fileSizeField = value;
                this.RaisePropertyChanged("FileSize");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool FileSizeSpecified
        {
            get
            {
                return this.fileSizeFieldSpecified;
            }
            set
            {
                this.fileSizeFieldSpecified = value;
                this.RaisePropertyChanged("FileSizeSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 7)]
        public string ContentType
        {
            get
            {
                return this.contentTypeField;
            }
            set
            {
                this.contentTypeField = value;
                this.RaisePropertyChanged("ContentType");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 8)]
        public Thumbnail Thumbnail
        {
            get
            {
                return this.thumbnailField;
            }
            set
            {
                this.thumbnailField = value;
                this.RaisePropertyChanged("Thumbnail");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public partial class Thumbnail :System.ComponentModel.INotifyPropertyChanged
    {

        private string urlField;

        private string contentTypeField;

        private uint widthField;

        private bool widthFieldSpecified;

        private uint heightField;

        private bool heightFieldSpecified;

        private uint fileSizeField;

        private bool fileSizeFieldSpecified;

        private uint runTimeField;

        private bool runTimeFieldSpecified;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
        public string Url
        {
            get
            {
                return this.urlField;
            }
            set
            {
                this.urlField = value;
                this.RaisePropertyChanged("Url");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
        public string ContentType
        {
            get
            {
                return this.contentTypeField;
            }
            set
            {
                this.contentTypeField = value;
                this.RaisePropertyChanged("ContentType");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 2)]
        public uint Width
        {
            get
            {
                return this.widthField;
            }
            set
            {
                this.widthField = value;
                this.RaisePropertyChanged("Width");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool WidthSpecified
        {
            get
            {
                return this.widthFieldSpecified;
            }
            set
            {
                this.widthFieldSpecified = value;
                this.RaisePropertyChanged("WidthSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 3)]
        public uint Height
        {
            get
            {
                return this.heightField;
            }
            set
            {
                this.heightField = value;
                this.RaisePropertyChanged("Height");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool HeightSpecified
        {
            get
            {
                return this.heightFieldSpecified;
            }
            set
            {
                this.heightFieldSpecified = value;
                this.RaisePropertyChanged("HeightSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 4)]
        public uint FileSize
        {
            get
            {
                return this.fileSizeField;
            }
            set
            {
                this.fileSizeField = value;
                this.RaisePropertyChanged("FileSize");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool FileSizeSpecified
        {
            get
            {
                return this.fileSizeFieldSpecified;
            }
            set
            {
                this.fileSizeFieldSpecified = value;
                this.RaisePropertyChanged("FileSizeSpecified");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 5)]
        public uint RunTime
        {
            get
            {
                return this.runTimeField;
            }
            set
            {
                this.runTimeField = value;
                this.RaisePropertyChanged("RunTime");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool RunTimeSpecified
        {
            get
            {
                return this.runTimeFieldSpecified;
            }
            set
            {
                this.runTimeFieldSpecified = value;
                this.RaisePropertyChanged("RunTimeSpecified");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
